package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.SpreadContentMapper;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.model.Spread;
import com.umakr.ax.common.model.SpreadContent;
import com.umakr.ax.common.service.RadioStationService;
import com.umakr.ax.common.service.SpreadContentService;
import com.umakr.ax.common.service.SpreadService;
import com.umakr.ax.common.vo.SpreadContentVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class SpreadContentWebService extends BaseService<SpreadContentMapper, SpreadContent> {
    @Autowired
    SpreadContentService spreadContentService;
    @Autowired
    RadioStationService radioStationService;
    @Autowired
    SpreadService spreadService;

    @Autowired
    Environment env;


    public Page<SpreadContentVO> listSpreadResult(Long rsId, Long spreadId, String searchText) {
        if("".equals(searchText)) {
            searchText=null;
        }
        Page<SpreadContentVO> page = baseMapper.listSpreadResult(rsId,spreadId,searchText);
        return page;
    }

    public Object getSpreadContentById(Long id) {
        SpreadContent spreadContent = baseMapper.selectById(id);
        Map<String,Object> map = Maps.newHashMap();
        if(spreadContent != null){
            map.put("id",spreadContent.getId());
            map.put("rsId",spreadContent.getRsId());
            map.put("activityTitle",spreadContent.getActivityTitle());
            map.put("templatePc",spreadContent.getTemplatePc());
        }else {
            map.put("id",id);
            map.put("rsId","");
            map.put("activityTitle","");
            map.put("templatePc","");
        }
        return resultMap(map);
    }

    public Object createSpreadContent(Long id , Long rsId,String title,String content){
        Map<String,Object> map = Maps.newHashMap();
        Date date = new Date();
        SpreadContent sc;
        if(id == null){
            sc = new SpreadContent();
        }else{
            sc = queryById(id);
        }

        if(sc != null){
            RadioStation rs = radioStationService.queryById(rsId);
            if(rs != null) {
                sc.setRsId(rsId);
                sc.setCreateTime(date);
                sc.setUpdateTime(date);
                sc.setActivityTitle(title);
                sc.setTemplatePc(content);
                sc  = update(sc);

                String uploadPath = makeSpreadContent(sc,content);
                sc.setMobileUrl(uploadPath);
                sc = update(sc);

                map.put("id",sc.getId());
                map.put("rsId",sc.getRsId());
                map.put("activityTitle",sc.getActivityTitle());
                map.put("templatePc",sc.getTemplatePc());

            }else{
                return resultMap(0,"错误");
            }
        }else{
            return resultMap(0,"错误");
        }
        return resultMap(map,1,"创建成功");
    }

    public Object delSpreadContent(Long id) {
        int errorCode = 0 ;
        String msg = "操作失败";

        SpreadContent spreadContent = baseMapper.selectById(id);
        if(spreadContent != null){
            if(null == spreadContent.getSpreadId()){
                baseMapper.deleteById(spreadContent.getId());
                errorCode = 1 ;
                msg = "删除成功";
            }else {
                Spread spread = spreadService.selectById(spreadContent.getSpreadId());
                if(spread != null){
                    Date endDate = spread.getSpreadExpireEnd();
                    Date curDate = new Date();
                    if(curDate.before(endDate)){
                        errorCode = 0 ;
                        msg = "删除失败，该活动正在进行中";
                    }else {
                        baseMapper.deleteById(spreadContent.getId());
                        errorCode = 1 ;
                        msg = "删除成功";
                    }
                }else {
                    baseMapper.deleteById(spreadContent.getId());
                    errorCode = 1 ;
                    msg = "删除成功";
                }
            }
        }else {
            errorCode = 0 ;
            msg = "删除失败，该活动已被删除";
        }
        return  resultMap(errorCode,msg);
    }



    private String makeSpreadContent(SpreadContent sc ,String content){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        FreeMarkerUtil fm = new FreeMarkerUtil(env.getProperty("coupon.static.html.path"));
        Map<String,Object> map = Maps.newHashMap();
        map.put("title",sc.getActivityTitle());
        map.put("templateContent", content);
        String savePath = "spreadCount" + File.separator + sc.getId();
        fm.make(map,"spreadContent.ftl",savePath);
        String filePath = savePath + File.separator + "index.html";
        return filePath;
    }

    private String makeSpreadTmp(SpreadContent sc , String stationName,String content){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        FreeMarkerUtil fm = new FreeMarkerUtil(env.getProperty("coupon.static.html.path"));
        Map<String,Object> map = Maps.newHashMap();
        map.put("title",sc.getActivityTitle());
        map.put("time",sf.format(sc.getCreateTime()));
        map.put("sallerName", stationName);
        map.put("content", content);

        String savePath = "spreadCount" + File.separator + sc.getId();
        fm.make(map,"mainTmp.ftl",savePath);
        String filePath = savePath + File.separator + "index.html";

        return filePath;
    }
}
